package com.ruoyi.teacher.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.teacher.domain.TAwardAuthor;
import com.ruoyi.teacher.mapper.TAwardMapper;
import com.ruoyi.teacher.domain.TAward;
import com.ruoyi.teacher.service.ITAwardService;

/**
 * 获奖信息集Service业务层处理
 *
 * @author xiangzy
 * @date 2022-05-10
 */
@Service
public class TAwardServiceImpl implements ITAwardService
{
    @Autowired
    private TAwardMapper tAwardMapper;

    /**
     * 查询获奖信息集
     *
     * @param recordid 获奖信息集ID
     * @return 获奖信息集
     */
    @Override
    public TAward selectTAwardById(Long recordid)
    {
        return tAwardMapper.selectTAwardById(recordid);
    }

    /**
     * 查询获奖信息集列表
     *
     * @param tAward 获奖信息集
     * @return 获奖信息集
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<TAward> selectTAwardList(TAward tAward)
    {
        return tAwardMapper.selectTAwardList(tAward);
    }

    /**
     * 新增获奖信息集
     *
     * @param tAward 获奖信息集
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTAward(TAward tAward)
    {
        tAward.setCreateTime(DateUtils.getNowDate());
        int rows = tAwardMapper.insertTAward(tAward);
        insertTAwardAuthor(tAward);
        return rows;
    }

    /**
     * 修改获奖信息集
     *
     * @param tAward 获奖信息集
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTAward(TAward tAward)
    {
        tAward.setUpdateTime(DateUtils.getNowDate());
        tAwardMapper.deleteTAwardAuthorByAWARDID(tAward.getRecordid());
        insertTAwardAuthor(tAward);
        return tAwardMapper.updateTAward(tAward);
    }

    /**
     * 批量删除获奖信息集
     *
     * @param recordids 需要删除的获奖信息集ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTAwardByIds(Long[] recordids)
    {
        tAwardMapper.deleteTAwardAuthorByAWARDIDs(recordids);
        return tAwardMapper.deleteTAwardByIds(recordids);
    }

    /**
     * 删除获奖信息集信息
     *
     * @param recordid 获奖信息集ID
     * @return 结果
     */
    @Override
    public int deleteTAwardById(Long recordid)
    {
        tAwardMapper.deleteTAwardAuthorByAWARDID(recordid);
        return tAwardMapper.deleteTAwardById(recordid);
    }

    /**
     * 新增获奖作者信息集信息
     *
     * @param tAward 获奖信息集对象
     */
    public void insertTAwardAuthor(TAward tAward)
    {
        List<TAwardAuthor> tAwardAuthorList = tAward.getTAwardAuthorList();
        Long recordid = tAward.getRecordid();
        if (StringUtils.isNotNull(tAwardAuthorList))
        {
            List<TAwardAuthor> list = new ArrayList<TAwardAuthor>();
            for (TAwardAuthor tAwardAuthor : tAwardAuthorList)
            {
                tAwardAuthor.setAwardid(recordid);
                list.add(tAwardAuthor);
            }
            if (list.size() > 0)
            {
                tAwardMapper.batchTAwardAuthor(list);
            }
        }
    }
}
